//
//  GoodsManagerViewController.m
//  LazyCat
//
//  Created by jike on 16/4/28.
//  Copyright (c) 2016年 zhanshu. All rights reserved.
//

#import "GoodsManagerViewController.h"
#import "firstTableViewCell.h"
//#import "ProductCell.h"
//#import "DataBase.h"
//#import "Product.h"
#import "FeWifiManHub.h"
#import "ProductCell.h"
#import "AddProductViewController.h"

#import "WebViewController.h" //商品管理之商品详情

@interface GoodsManagerViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    UIView *lineV;//滑动下面的那条线
    //    UIView *headerV;
    UIView *newNaviV;
    UIView *footerV;
    
    UIScrollView *secondSroV;//scrView
    UITableView *leftTabV;//左边tableView
    UITableView *rightTabV;//右边tableView
    NSInteger allLength;//scrView数组长度
    NSInteger tableMSele;//左边tableview点击位置
    NSInteger movePath;//滑动上移之后位置
    NSInteger highPath;//滑动上移之后位置
    UIButton *oldBtn;//scrView选中按钮
    NSMutableArray *scrArr;//scrView数组
    NSMutableArray *dataArr1;//左边表视图数组
    NSMutableArray *dataArr2;//右边表视图数组
    NSMutableArray *bigArr;//大数组为取所有产品
    
    UIView *noproV;//没有产品视图
    UIButton *jzsbBtn;//一级分类加载失败按钮
    FeWifiManHub *_loadingAniView;//加载框
    NSMutableArray *loadArr;//加载框数组
    CGSize size;//长宽
    
    NSMutableArray *_productArray;//产品列表数组
    NSString *_isPutOnGoods;//是否为上架商品
    NSString *_proID;//id
    NSIndexPath *_indexPath;
    NSString *_isHot;//是否为推荐
    NSString *_proName;
    NSString *_proImg;
    NSString *_isGetUpGoods;//是否为上架,接口
    NSInteger _itemIndex;//右边上方项的点击位置
    NSString *_indexUrl;//网址起始....
    BOOL _isLoading;
    NSString *_totalCount;
    BOOL _freshData;//点击左边分类，清空元数据
    NSMutableArray *_sccondCategoryArr;//二级分类数组
    BOOL _isAddAllProduct;//默认第一次添加..

}

@end

@implementation GoodsManagerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createUI];
    [self getCategoryData];
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    if ([AppData share].isLoadd == YES) {
        NSArray  *arr;
        NSString *cid;
        if (dataArr1.count > 0)
        {arr = [dataArr1[tableMSele] objectForKey:@"childrencategory"];
            if     (_itemIndex == 0)
            {
                cid = [dataArr1[tableMSele] objectForKey:@"categoryid"];
            }
            else
            {
                cid = [arr[_itemIndex - 1] objectForKey:@"categoryid"];
            }
        }
        [self getProductListByID:cid];
    }
    [AppData share].isLoadd = NO;
    
}
- (void)createUI {
    [self initNav:@"" doBack:YES];
    NSArray *titles=@[@"在售商品",@"已下架商品"];
    UISegmentedControl *seg=[[UISegmentedControl alloc]initWithItems:titles];
    seg.frame=CGRectMake((ScreenWidth - 154)/2, 27, 154, 29);
    [self.navBar addSubview:seg];
    seg.tintColor = MainColorT;
    seg.selectedSegmentIndex = 0;
    [seg addTarget:self action:@selector(changeSegment:) forControlEvents:UIControlEventValueChanged];
    //new
    loadArr = [NSMutableArray array];
    _productArray = [[NSMutableArray alloc] init];
    _sccondCategoryArr = [[NSMutableArray alloc]init];//全部产品
    [self createFooterView];
    _loadingAniView = [[FeWifiManHub alloc]initWithView:self.view withMode:FeWifiManHubModeOnlyLoader];
    [self.view addSubview:_loadingAniView];
    [_loadingAniView show];
    _isPutOnGoods = @"1";
    _itemIndex = 0;
    _indexUrl = @"1";
    _freshData = NO;
    _isAddAllProduct = YES;
    //[self getParentData];
}
#pragma mark -- 用户操作segmentedControl时的处理方法
-(void)changeSegment:(UISegmentedControl*)sender
{
    _loadingAniView = [[FeWifiManHub alloc]initWithView:self.view withMode:FeWifiManHubModeOnlyLoader];
    [self.view addSubview:_loadingAniView];
    [_loadingAniView show];
    //获取用户选择的分段项的下标
    NSInteger index= sender.selectedSegmentIndex;
    //获取下标处的分段项的标题
    NSString *title=[sender titleForSegmentAtIndex:index];
    if (index == 0) {
        _isPutOnGoods = @"1";
    }else {
        _isPutOnGoods = @"0";
    }
    NSArray  *arr;
    NSString *cid;
    if (dataArr1.count > 0)
    {arr = [dataArr1[tableMSele] objectForKey:@"childrencategory"];
        if     (_itemIndex == 0)
        {
            cid = [dataArr1[tableMSele] objectForKey:@"categoryid"];
        }
        else
        {
            cid = [arr[_itemIndex - 1] objectForKey:@"categoryid"];
        }
    }
    [self getProductListByID:cid];
}
/**
 *  getdata
 */
#pragma mark -- NEW 获取项数据 -------=-=-=-=-
- (void)getCategoryData {
    if (kNetworkNotReachability) {
        [[MessShowView share]showWithMess:@"当前网络不可用"];
        noproV.hidden = NO;
        jzsbBtn.hidden = NO;
    }else {
    NSString *url = [NSString stringWithFormat:GoodsManagerCategoryUrl,[AppData share].username];
    [AFNetworkHandler GETWithAFNByURL:url completion:^(id result) {
        NSDictionary *dic = result;
        if ([dic[@"r"] isEqualToString:@"T"]) {
            dataArr1 = [NSMutableArray array];
            dataArr2 = [NSMutableArray array];
            NSArray *arrFir = [dic objectForKey:@"categories"];
            [dataArr1 addObjectsFromArray:arrFir];
            if (dataArr1.count > 0) {
                for (int i = 0; i < dataArr1.count; i++) {
//                    NSArray *arrTwo = [dataArr1[i] objectForKey:@"childrencategory"];
//                    [dataArr2 addObject:arrTwo];
                    if (i == tableMSele) {
                        [loadArr insertObject:@"1" atIndex:i];
                    }else {
                        [loadArr insertObject:@"0" atIndex:i];
                    }
                }
            }
        }
        [_loadingAniView dismiss];
        [leftTabV reloadData];
        
        if (dataArr1.count > 0)
        {
            NSString *cid= [dataArr1[0] objectForKey:@"categoryid"];
            [self getProductListByID:cid];
            [self addAllBtn];
            
            noproV.hidden = YES;
            jzsbBtn.hidden = YES;
        }else
        {
            noproV.hidden = NO;
            jzsbBtn.hidden = NO;
        }
        [rightTabV reloadData];
    }];
    }
}
#pragma mark -- NEW 刷新当前修改行
- (void)refreshCurrentLineWithCid:(NSString *)cid withIndexP:(NSIndexPath *)indexPath{
    
}
#pragma mark -- NEW 获取商品列表 ----====----==--=
- (void)getProductListByID:(NSString *)cid {
    if ([_indexUrl isEqualToString:@"1"] || _freshData == YES) {
        if (_productArray.count > 0) {
            [_productArray removeAllObjects];
        }
        if (dataArr2.count > 0) {
            [dataArr2 removeAllObjects];
        }
        _freshData = NO;
    }
    NSString *kurl = [NSString stringWithFormat:GoodsManagerListUrl,[AppData share].username,cid,_isPutOnGoods,@"10",_indexUrl];
    [AFNetworkHandler GETWithAFNByURL:kurl completion:^(id result) {
        NSDictionary *dic = result;
        if ([dic[@"r"] isEqualToString:@"T"])
        {
            NSArray *productArr = dic[@"products"];//返回产品数组
            [dataArr2 addObjectsFromArray:productArr];
            _totalCount = dic[@"totalpage"];
            
            if (dataArr2.count > 0) {
                noproV.hidden = YES;
            }else {
                noproV.hidden = NO;
            }
            [_loadingAniView dismiss];
            [rightTabV.header endRefreshing];
            [rightTabV.footer endRefreshing];
            [rightTabV reloadData];
            _isLoading = NO;
        }else {
            [_loadingAniView dismiss];
        }
    }];
}

//#pragma mark -- NEW 获取商品列表 ----====----==--=
//- (void)getProductListByID:(NSString *)cid {
//    [self addAllBtn];
//    if ([_indexUrl isEqualToString:@"1"] || _freshData == YES) {
//        if (_productArray.count > 0) {
//            [_productArray removeAllObjects];
//        }
//        if (dataArr2.count > 0) {
//            [dataArr2 removeAllObjects];
//        }
//        if (_AllProductArr.count > 0) {
//            [_AllProductArr removeAllObjects];
//        }
//        _freshData = NO;
//    }
//    NSString *kurl = [NSString stringWithFormat:GoodsManagerListUrl,[AppData share].username,cid,_isPutOnGoods,@"10",_indexUrl];
//    [AFNetworkHandler GETWithAFNByURL:kurl completion:^(id result) {
//        NSDictionary *dic = result;
//        if ([dic[@"r"] isEqualToString:@"T"])
//        {
//            NSArray *productArr = dic[@"products"];//返回产品数组
//            NSArray *subCatesArr = dic[@"subcates"];//返货的分类数组
//            _totalCount = dic[@"totalpage"];
//            [_AllProductArr addObjectsFromArray:productArr];
////            if (_isAddAllProduct == YES) {//第一次处理的时候添加上全部的数据
////                [_productArray addObject:productArr];
////                _isAddAllProduct = NO;
////            }
//            if (_productArray.count > 0) {
//                [_productArray removeAllObjects];
//            }
//            if (dataArr2.count > 0) {
//                [dataArr2 removeAllObjects];
//            }
//            [_productArray addObject:_AllProductArr];
//            for (int i = 0; i< subCatesArr.count; i++)
//            {
//                NSMutableArray *productCatesArr = [[NSMutableArray alloc]init];
//                NSString *cateid = subCatesArr[i][@"cateid"];
//                for (int j = 0; j < _AllProductArr.count; j++) {
//                    if ([[_AllProductArr[j] objectForKey:@"categoryid"] isEqualToString:cateid])
//                    {
//                        [productCatesArr addObject:_AllProductArr[j]];
//                    }
//                }
//                [_productArray addObject:productCatesArr];
//            }
//            [dataArr2 addObjectsFromArray:_productArray[_itemIndex]];
//            NSLog(@"%@",dataArr2);
////            NSLog(@"%D",_itemIndex);
//            if (dataArr2.count > 0) {
//                noproV.hidden = YES;
//            }else {
//                noproV.hidden = NO;
//            }
//            [rightTabV.header endRefreshing];
//            [rightTabV.footer endRefreshing];
//            [rightTabV reloadData];
//            _isLoading = NO;
//        }else {
//        }
//    }];
//}
#pragma mark - 创建下面视图
-(void)createFooterView {
    footerV = [[UIView alloc]initWithFrame:CGRectMake(0, 64, ScreenWidth, ScreenHeight-64)];
    footerV.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:footerV];
    [self createMainView];
}
#pragma mark - 创建主视图界面
-(void)createMainView {
    movePath = 0;
    highPath = 0;
    allLength = 0;
    tableMSele = 0;
    _isLoading = NO;
    leftTabV = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 75*ScreenWidth/320, ScreenHeight-64 - 44) style:UITableViewStyleGrouped];
    leftTabV.backgroundColor = SetColor(245,245,245, 1);
    leftTabV.tag = 111;
    leftTabV.delegate = self;
    leftTabV.dataSource = self;
    leftTabV.showsVerticalScrollIndicator = NO;
    leftTabV.separatorStyle = UITableViewCellSeparatorStyleNone;
    [footerV addSubview:leftTabV];
    [leftTabV registerClass:[firstTableViewCell class] forCellReuseIdentifier:@"Cell1"];
    
    jzsbBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    jzsbBtn.backgroundColor = [UIColor clearColor];
    jzsbBtn.hidden = YES;
    [jzsbBtn setTitle:@"点击加载" forState:UIControlStateNormal];
    [jzsbBtn setTitleColor:SetColor(153, 153, 153, 1) forState:UIControlStateNormal];
    jzsbBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    //[jzsbBtn setTitleColor:SetColor(0, 0, 0, 1) forState:UIControlStateNormal];
    jzsbBtn.frame = CGRectMake(0, (leftTabV.frame.size.height-30)/2, ScreenWidth/4, 30);
    [jzsbBtn addTarget:self action:@selector(shuaxin) forControlEvents:UIControlEventTouchUpInside];
    [leftTabV addSubview:jzsbBtn];
    
    rightTabV = [[UITableView alloc]initWithFrame:CGRectMake(leftTabV.size.width, 34, 245 * ScreenWidth/320, ScreenHeight-64-35 - 44) style:UITableViewStyleGrouped];
    rightTabV.backgroundColor = [UIColor whiteColor];
    rightTabV.tag = 222;
    rightTabV.delegate = self;
    rightTabV.dataSource = self;
    rightTabV.showsVerticalScrollIndicator = NO;
    [footerV addSubview:rightTabV];
    rightTabV.separatorStyle = UITableViewCellSeparatorStyleNone;
//    [rightTabV registerClass:[ProductCell class] forCellReuseIdentifier:@"Cell2"];
    rightTabV.header = [MJDIYHeader headerWithRefreshingTarget:self refreshingAction:@selector(refreshSecond)];
    MJRefreshAutoFooter *foot = [MJRefreshAutoFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMore)];
    rightTabV.footer = foot;
    //头上的滚动条
    secondSroV = [[UIScrollView alloc]initWithFrame:CGRectMake(leftTabV.frame.size.width, 0, 3*rightTabV.frame.size.width, 35)];
    secondSroV.backgroundColor = [UIColor whiteColor];
    secondSroV.tag = 200;
    secondSroV.delegate = self;
    secondSroV.showsHorizontalScrollIndicator = NO;
    [footerV addSubview:secondSroV];
    
    //滚动条上面的横线
    UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(leftTabV.frame.size.width, 0, ScreenWidth - leftTabV.frame.size.width, 1)];
    lineView.backgroundColor = SetColor(153, 153, 153, 0.2);
    [footerV addSubview:lineView];
    
    noproV = [[UIView alloc]initWithFrame:CGRectMake(0, 100, 3*ScreenWidth/4, 100)];
    noproV.backgroundColor = [UIColor clearColor];
    noproV.hidden = YES;
    [rightTabV addSubview:noproV];
    
    UIImageView *noProImV = [[UIImageView alloc]initWithFrame:CGRectMake((3*ScreenWidth/4-49)/2, 15, 49, 49)];
    noProImV.image = [UIImage imageNamed:@"noOrder"];
    [noproV addSubview:noProImV];
    
    UILabel *noProMess = [[UILabel alloc]initWithFrame:CGRectMake(0, 70, 3*ScreenWidth/4, 20)];
    noProMess.textColor = SetColor(170, 170, 170, 1);
    noProMess.textAlignment = NSTextAlignmentCenter;
    noProMess.font = [UIFont systemFontOfSize:14];
    noProMess.text = @"暂无该相关产品";
    [noproV addSubview:noProMess];
    
    //保存
    UIView *baocunView = [[UIView alloc]initWithFrame:CGRectMake(0, ScreenHeight - 44, ScreenWidth, 44)];
    baocunView.backgroundColor = MainColorT;
    
    [self.view addSubview:baocunView];
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(0, 0, ScreenWidth, 44);
    [btn setTitle:@"添加商品" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(baocunBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [baocunView addSubview:btn];
}
#pragma mark -- 添加商品.....
- (void)baocunBtnClick:(UIButton *)sender {
    AddProductViewController *addVC = [[ AddProductViewController alloc]init];
    addVC.addVCStyle = LCProductStyleAdd;
    [self.navigationController pushViewController:addVC animated:YES];
}
//加载失败刷新
-(void)shuaxin {
    //[NSUserDefualManager storageValue:@[] forKey:@"LC_StorageMarketMess"];
    //[self getpareData11];
    if (kNetworkNotReachability) {
        
    }else {
        [self getCategoryData];
    }
}
#pragma mark --上啦加载
- (void)loadMore {
    if (_isLoading) {
        return;
    }
    _isLoading = YES;
    if ([_indexUrl integerValue] >= [_totalCount integerValue]) {
        [[MessShowView share]showWithMess:@"没有更多数据"];
        return ;
    }
    _indexUrl = [NSString stringWithFormat:@"%ld",[_indexUrl integerValue]+1];
    NSArray  *arr;
    NSString *cid;
    if (tableMSele < [dataArr1 count]) {
        arr = [dataArr1[tableMSele] objectForKey:@"childrencategory"];
        if     (_itemIndex == 0)
        {
            cid = [dataArr1[tableMSele] objectForKey:@"categoryid"];
        }
        else
        {
            cid = [arr[_itemIndex - 1] objectForKey:@"categoryid"];
        }
        [self getProductListByID:cid];
    }
}
#pragma mark - 上拉下拉
- (void)refreshSecond {
    //[self getpareData11];
    if (_isLoading) {
        return;
    }
    _isLoading = YES;
    _indexUrl = @"1";
    NSArray  *arr;
    NSString *cid;
    if (tableMSele < [dataArr1 count]) {
        arr = [dataArr1[tableMSele] objectForKey:@"childrencategory"];
        if     (_itemIndex == 0)
        {
            cid = [dataArr1[tableMSele] objectForKey:@"categoryid"];
        }
        else
        {
            cid = [arr[_itemIndex - 1] objectForKey:@"categoryid"];
        }
        [self getProductListByID:cid];
        [AppData share].isLoadd = YES;//改变刷新
    }
    [rightTabV.header endRefreshing];
}
-(void)getpareData11 {
    if (kNetworkNotReachability) { // 没有网
        [[MessShowView share] showWithMess:@"当前网络不给力"];
        [dataArr1 removeAllObjects];
        [dataArr2 removeAllObjects];
        secondSroV.hidden = YES;
        noproV.hidden = NO;
        jzsbBtn.hidden = NO;
        [leftTabV reloadData];
        [rightTabV reloadData];
        return;
    }else {
        jzsbBtn.hidden = YES;
        secondSroV.hidden = NO;
    }
}
#pragma mark - 遍历添加所有按钮
-(void)addAllBtn {
    for (int i = 0; i < scrArr.count; i++) {
        UIButton *bt = (UIButton *)[self.view viewWithTag:i+10000];
        [bt removeFromSuperview];
    }
    scrArr = [NSMutableArray array];
    NSArray *arr1 = [NSArray array];
    if (_sccondCategoryArr.count > 0) {
        [_sccondCategoryArr removeAllObjects];
    }
    if (dataArr1.count > 0) {
        arr1 = [dataArr1[tableMSele] objectForKey:@"childrencategory"];
        [_sccondCategoryArr addObjectsFromArray:arr1];
    }
    for (int i = 0; i < arr1.count; i++) {
        [scrArr addObject:[arr1[i] objectForKey:@"categoryname"]];
    }
    [scrArr insertObject:@"全部" atIndex:0];
    allLength = 0;
    for (int i = 0; i < scrArr.count; i++) {
        CGSize titleSize = [scrArr[i] sizeWithFont:[UIFont systemFontOfSize:12] constrainedToSize:CGSizeMake(MAXFLOAT, 35)];
        UIButton *classBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        classBtn.frame = CGRectMake(17.5*(i+1)+allLength, 0, titleSize.width, 35);
        allLength += titleSize.width;
        [classBtn setTitle:[NSString stringWithFormat:@"%@",scrArr[i]] forState:UIControlStateNormal];
        [classBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        classBtn.titleLabel.font = [UIFont systemFontOfSize:12];
        classBtn.tag = 10000+i;
        [classBtn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
        [secondSroV addSubview:classBtn];
        if (i == _itemIndex) {
            [classBtn setTitleColor:MainColorT forState:UIControlStateNormal];
            oldBtn = classBtn;
        }else {
            [classBtn setTitleColor:SetColor(76, 76, 76, 1) forState:UIControlStateNormal];
        }
    }
    NSInteger xhxL = (allLength+17.6*scrArr.count+17.6>3*ScreenWidth/4)?allLength+17.6*scrArr.count:3*ScreenWidth/4;
    secondSroV.contentSize = CGSizeMake(xhxL, 35);
    UIView *bomLV = [[UIView alloc]initWithFrame:CGRectMake(10, 34.5, xhxL, 0.5)];
    bomLV.backgroundColor = SetColor(219,214,214,1);
    [secondSroV addSubview:bomLV];
}
#pragma mark - scr点击事件
-(void)click:(UIButton *)sender {
    //NSLog(@"点击二级第%ld个分类",(long)sender.tag);
    _indexUrl = @"1";
    _itemIndex = sender.tag - 10000;
    if (sender != oldBtn) {
        _loadingAniView = [[FeWifiManHub alloc]initWithView:self.view withMode:FeWifiManHubModeOnlyLoader];
        [self.view addSubview:_loadingAniView];
        [_loadingAniView show];
        [oldBtn setTitleColor:SetColor(76, 76, 76, 1) forState:UIControlStateNormal];
        [sender setTitleColor:MainColorT forState:UIControlStateNormal];
        oldBtn = sender;
        //点击按钮滚动到中间
        CGFloat offsetX = sender.center.x - 3*ScreenWidth/8;
        if (offsetX < 0) { // 处理最小滚动偏移量
            offsetX = 0;
        }
        CGFloat maxOffsetX = secondSroV.contentSize.width - 3*ScreenWidth/4;
        if (offsetX > maxOffsetX) { // 处理最大滚动偏移量
            offsetX = maxOffsetX;
        }
        [secondSroV setContentOffset:CGPointMake(offsetX, 0) animated:YES];
        if (dataArr2.count > 0) {
            [dataArr2 removeAllObjects];
        }
//        NSArray *arr;
//        if (sender.tag - 10000 < [_productArray count]) {
//            arr = _productArray[sender.tag - 10000];
//        }
//        [dataArr2 addObjectsFromArray:arr];
//        if (dataArr2.count > 0) {
//            noproV.hidden = YES;
//        }else {
//            noproV.hidden = NO;
//        }
//        [rightTabV reloadData];
        
        //NEW..
        NSArray  *arr;
        NSString *cid;
        if (dataArr1.count > 0)
        {arr = [dataArr1[tableMSele] objectForKey:@"childrencategory"];
            if     (_itemIndex == 0)
            {
                cid = [dataArr1[tableMSele] objectForKey:@"categoryid"];
            }
            else
            {
                cid = [arr[_itemIndex - 1] objectForKey:@"categoryid"];
            }
        }
        [self getProductListByID:cid];
        
        
        

        
//        if (sender.tag == 10000) {
//            for (int i = 0; i < bigArr.count; i++) {
//                [dataArr2 addObjectsFromArray:[bigArr[i] objectForKey:@"products"]];
//            }
//            if (dataArr2.count == 0) {
//                noproV.hidden = NO;
//            }else {
//                noproV.hidden = YES;
//            }
//            [rightTabV reloadData];
//        }else {
//            if (dataArr1.count > 0) {
//                NSArray *arr1 = [NSArray arrayWithArray:[dataArr1[tableMSele] objectForKey:@"childrencategory"]];
//                if (arr1.count > 0) {
//                    NSString *secId = [arr1[sender.tag-10000-1] objectForKey:@"categoryid"];
//                    for (int i = 0; i < bigArr.count; i++) {
//                        if ([secId isEqualToString:[bigArr[i] objectForKey:@"categoryid"]]) {
//                            dataArr2 = [NSMutableArray arrayWithArray:[bigArr[i] objectForKey:@"products"]];
//                        }
//                    }
//                    if (dataArr2.count == 0) {
//                        noproV.hidden = NO;
//                    }else {
//                        noproV.hidden = YES;
//                    }
//                    [rightTabV reloadData];
//                }
//            }
//        }
        
    }else {
        
    }
}
#pragma mark -- 上下架等三个按钮点击事件
- (void)btnClick:(UIButton *)sender event:(id)event {
    //取indexpath
    NSSet *touches =[event allTouches];
    UITouch *touch =[touches anyObject];
    CGPoint currentTouchPosition = [touch locationInView:rightTabV];
    NSIndexPath *indexPath= [rightTabV indexPathForRowAtPoint:currentTouchPosition];
    _indexPath = indexPath;
    if (indexPath.row < [dataArr2 count]) {
        _proID = [dataArr2[indexPath.row]objectForKey:@"userproductid"];
    }
    if (indexPath!= nil)
    {
//        NSLog(@"我在第%ld行，编号%ld",(long)indexPath.row,(long)sender.tag);
        if (sender.tag == 1111) {//上下架
            if (sender.selected == NO) {
                _isGetUpGoods = @"0";
            }else if (sender.selected == YES){
                _isGetUpGoods = @"1";
            }
            if (indexPath.row < [dataArr2 count]) {
                _proID = [dataArr2[indexPath.row]objectForKey:@"userproductid"];
            }
            NSString *url = [NSString stringWithFormat:shangXiaJiaProduct,_proID,_isGetUpGoods];
            [NTNetUtil sendRequestWithUrl:url requestName:nil method:NTRequestMethodGET parameters:nil block:^(NTHttpRequest *networkRequest, NSError *error) {
                NSDictionary *dic = networkRequest.responseData;
                NSString *result = [dic objectForKey:@"m"];
                if ([result isEqualToString:@"success"]) {
                    if ([_isGetUpGoods isEqualToString:@"1"]) {
                        [[MessShowView share]showWithMess:@"商品上架成功" login:YES];
                    }else if ([_isGetUpGoods isEqualToString:@"0"]){
                        [[MessShowView share]showWithMess:@"商品下架成功" login:YES];
                    }
#warning --刷新问题
                    if (indexPath.row < dataArr2.count) {
                        [dataArr2 removeObjectAtIndex:indexPath.row];
                    }
                    [rightTabV reloadData];
                    [AppData share].isLoadd = YES;//改变刷新
                }else  {
                    [[MessShowView share]showWithMess:result login:NO];
                }
            }];
        }else if (sender.tag == 1112){//设为推荐
            if (indexPath.row < [dataArr2 count]) {
                _isHot = dataArr2[indexPath.row][@"ishot"];
            }
            if (indexPath.row < [dataArr2 count]) {
                _proID = [dataArr2[indexPath.row]objectForKey:@"userproductid"];
            }
            NSString *url;
            if ([_isHot isEqualToString:@"1"]) {
                url = [NSString stringWithFormat:isRecommend,_proID,@""];
            }else {
                url = [NSString stringWithFormat:isRecommend,_proID,@"1"];
            }
            [NTNetUtil sendRequestWithUrl:url requestName:nil method:NTRequestMethodGET parameters:nil block:^(NTHttpRequest *networkRequest, NSError *error) {
                NSDictionary *dic = networkRequest.responseData;
                NSString *result = [dic objectForKey:@"m"];
                if ([result isEqualToString:@"success"]) {
                    //[self loadDataBBBBB:_indexPath];
                    [[MessShowView share]showWithMess:@"操作成功" login:YES];
#warning --推荐刷新当前行
                    NSMutableDictionary *mutableDic;
                    if (indexPath.row < dataArr2.count) {
                        mutableDic = [NSMutableDictionary dictionaryWithDictionary:dataArr2[indexPath.row]];
                    }
                    if ([mutableDic.allKeys containsObject:@"ishot"]) {
                        NSString *isH = mutableDic[@"ishot"];
                        if   ([isH isEqualToString:@"0"]) {isH = @"1";}
                        else {isH = @"0";}
                        [mutableDic removeObjectForKey:@"ishot"];
                        [mutableDic setObject:isH forKey:@"ishot"];
                    }
                    [dataArr2 replaceObjectAtIndex:indexPath.row withObject:mutableDic];
                    [rightTabV reloadData];
                }else  {
                    [[MessShowView share]showWithMess:result login:NO];
                }
            }];
        }else if (sender.tag == 1113){
            if (indexPath.row < [dataArr2 count]) {
                _proID = [dataArr2[indexPath.row]objectForKey:@"userproductid"];
                _proName = [dataArr2[indexPath.row]objectForKey:@"productname"];
                _proImg = [dataArr2[indexPath.row]objectForKey:@"img"];
            }
            [self fenXiangAction];
        }
    }
}
// 分享
-(void)fenXiangAction {
    NSString *url = [NSString stringWithFormat:@"%@/LCat/Product-%@?appname=lanmao&plat=ios&username=%@&iszhanggui=1&sign2=%@",BaseUrl,_proID,[AppData share].username,[Utils md5SignStr]];
    [[ShareView share] showMess:[NSString stringWithFormat:@"%@%@",_proName,url] withImgUrl:_proImg url:url];
}

//- (void)threeBtnClick:(UIButton *)sender {
//    if (sender.tag == 111) {//上下架
//        NSString *url = [NSString stringWithFormat:@""];
//        [AFNetworkHandler GETWithAFNByURL:url completion:^(id result) {
//            
//        }];
//        NSLog(@"12345");
//    }else if (sender.tag == 112){//推荐
//        NSString *url = [NSString stringWithFormat:@""];
//        [AFNetworkHandler GETWithAFNByURL:url completion:^(id result) {
//            
//        }];
//        NSLog(@"333");
//    }else if (sender.tag == 113){//分享
//        
//    }
//}
#pragma uitableView datasource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (tableView.tag == 111) {
        return dataArr1.count;
    }else {
        return dataArr2.count;
    }
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView.tag == 111) {
        firstTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell1"];
        if (cell == nil) {
            cell = [[firstTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell1"];
        }
        if (indexPath.row == 0) {
            cell.top1V.hidden = NO;
        }else {
            cell.top1V.hidden = YES;;
        }
        if (indexPath.row == tableMSele) {
            cell.seletedLefV.hidden = NO;
            cell.seletedRigV.hidden = YES;
            cell.backgroundColor = [UIColor whiteColor];
            cell.name1Label.textColor = MainColorT;
        }else {
            cell.seletedLefV.hidden = YES;
            cell.seletedRigV.hidden = NO;
            cell.backgroundColor = SetColor(245,245,245, 1);
            cell.name1Label.textColor = SetColor(51, 51, 51, 1);
        }
        cell.name1Label.text = [NSString stringWithFormat:@"%@",[dataArr1[indexPath.row] objectForKey:@"categoryname"]];
        return cell;
    }else {
        ProductCell *cell = [tableView dequeueReusableCellWithIdentifier:@"productcell"];
        if (cell == nil) {
            cell = [[NSBundle mainBundle]loadNibNamed:@"ProductCell" owner:self options:nil][0];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
       
        cell.shouJiaTextF.adjustsFontSizeToFitWidth = YES;
        cell.chengBenTextF.adjustsFontSizeToFitWidth = YES;
        
        if ([_isPutOnGoods isEqualToString:@"1"]) {//上下架按钮
//            cell.upOrDownBtn.selected = NO;
        }else {
//            cell.upOrDownBtn.selected = YES;
        }
        
        /**
         *  推荐按钮
         */
//        NSLog(@"%d",[[dataArr2[indexPath.row] objectForKey:@"ishot"] integerValue]);
        if (indexPath.row < [dataArr2 count]) {
            if ([[dataArr2[indexPath.row] objectForKey:@"ishot"] integerValue] == 0) {
//                [cell.recommendBtn setImage:[UIImage imageNamed:@"btn_tuijian_huise"] forState:UIControlStateNormal];
            }else {
//                [cell.recommendBtn setImage:[UIImage imageNamed:@"btn_tuijian_zhutise"] forState:UIControlStateNormal];
            }
        }
        /**
         *  赋值
         */
        if (indexPath.row < [dataArr2 count]) {
            cell.titleLabel.text = [dataArr2[indexPath.row] objectForKey:@"productname"];
            [cell.iconImage sd_setImageWithURL:[dataArr2[indexPath.row]objectForKey:@"img"] placeholderImage:[UIImage imageNamed:@"product-no-pic"]];
            
            cell.shouJiaTextF.text = [dataArr2[indexPath.row] objectForKey:@"saleprice"];
            cell.chengBenTextF.text = [dataArr2[indexPath.row] objectForKey:@"costprice"];
            cell.guigeLabel.text = [dataArr2[indexPath.row] objectForKey:@"property"];
            cell.xiaoLiangLabel.text =[NSString stringWithFormat:@"销量:%@",[dataArr2[indexPath.row] objectForKey:@"numrealbuy"]];
            cell.kuCunLabel.text = [dataArr2[indexPath.row] objectForKey:@"numtotalstock"];
        }
        [cell.inBtnClicl addTarget:self action:@selector(inBtnClicl:event:) forControlEvents:UIControlEventTouchUpInside];
        [cell.inBtnClick2 addTarget:self action:@selector(inBtnClicl:event:) forControlEvents:UIControlEventTouchUpInside];
        return cell;
    }
}
- (void)inBtnClicl:(UIButton *)sender event:(id)event{
    //取indexpath
    NSSet *touches =[event allTouches];
    UITouch *touch =[touches anyObject];
    CGPoint currentTouchPosition = [touch locationInView:rightTabV];
    NSIndexPath *indexPath= [rightTabV indexPathForRowAtPoint:currentTouchPosition];
    _indexPath = indexPath;
    
    WebViewController *detailWeb = [[WebViewController alloc]init];
    if (kNetworkNotReachability) { // 没网
        [[MessShowView share] showWithMess:@"当前网络不给力"];
        return;
    }else {
        if (indexPath.row < [dataArr2 count]) {
            detailWeb.requestUrl = [dataArr2[indexPath.row]objectForKey:@"userproductid"];
            detailWeb.classID = [dataArr2[indexPath.row]objectForKey:@"categoryid"];
            detailWeb.className = [dataArr2[indexPath.row]objectForKey:@"categoryname"];
            detailWeb.isHot = [dataArr2[indexPath.row]objectForKey:@"ishot"];
            if ([_isPutOnGoods isEqualToString:@"1"]) {
                detailWeb.isShangJia = @"0";
            }else if ([_isPutOnGoods isEqualToString:@"0"]){
                detailWeb.isShangJia = @"1";
            }
            detailWeb.PName = [NSString stringWithFormat:@"%@",[dataArr2[indexPath.row]objectForKey:@"productname"]];
            detailWeb.PImg = [NSString stringWithFormat:@"%@",[dataArr2[indexPath.row]objectForKey:@"img"]];
            detailWeb.shoujiaStr = [dataArr2[indexPath.row]objectForKey:@"saleprice"];
            detailWeb.chengbenStr = [dataArr2[indexPath.row]objectForKey:@"costprice"];
            detailWeb.kucunStr = [dataArr2[indexPath.row]objectForKey:@"numtotalstock"];
            detailWeb.pid = [dataArr2[indexPath.row]objectForKey:@"userproductid"];
            [self.navigationController pushViewController:detailWeb animated:YES];
        }
    }
}
#pragma mark - 头部视图高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.01;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView.tag == 111) {
        return 44;
    }else {
        return 120;
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.01;
}
#pragma mark - tableV点击事件
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    _itemIndex = 0;
    _indexUrl = @"1";
    _isAddAllProduct = YES;
    if (tableView.tag == 111) {
        _loadingAniView = [[FeWifiManHub alloc]initWithView:self.view withMode:FeWifiManHubModeOnlyLoader];
        [self.view addSubview:_loadingAniView];
        [_loadingAniView show];
        _freshData = YES;
        NSLog(@"点击左边第%ld个",(long)indexPath.row);
        tableMSele = indexPath.row;
        [leftTabV scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
        allLength = 0;
        for (UIButton *bt in secondSroV.subviews) {
            [bt removeFromSuperview];
        }
        //[self getChildData11];
//        NSString *cid = [dataArr1[indexPath.row] [@"categoryid"];
        ;
        if (indexPath.row < [dataArr1 count]) {
             NSString *cid= [dataArr1[indexPath.row] objectForKey:@"categoryid"];
            [self getProductListByID:cid];
            [self addAllBtn];
        }
        
        [rightTabV setContentOffset:CGPointMake(0, 0)];
        [leftTabV reloadData];
    }else {
        NSLog(@"点击右边第%ld个",(long)indexPath.row);
//        WebViewController *detailWeb = [[WebViewController alloc]init];
//        if (kNetworkNotReachability) { // 没网
//            [[MessShowView share] showWithMess:@"当前网络不给力"];
//            return;
//        }else {
//            if (indexPath.row < [dataArr2 count]) {
//                detailWeb.requestUrl = [dataArr2[indexPath.row]objectForKey:@"userproductid"];
//                detailWeb.classID = [dataArr2[indexPath.row]objectForKey:@"categoryid"];
//                detailWeb.className = [dataArr2[indexPath.row]objectForKey:@"categoryname"];
//                detailWeb.isHot = [dataArr2[indexPath.row]objectForKey:@"ishot"];
//                if ([_isPutOnGoods isEqualToString:@"1"]) {
//                    detailWeb.isShangJia = @"0";
//                }else if ([_isPutOnGoods isEqualToString:@"0"]){
//                    detailWeb.isShangJia = @"1";
//                }
//                detailWeb.PName = [NSString stringWithFormat:@"%@",[dataArr2[indexPath.row]objectForKey:@"productname"]];
//                detailWeb.PImg = [NSString stringWithFormat:@"%@",[dataArr2[indexPath.row]objectForKey:@"img"]];
//                detailWeb.shoujiaStr = [dataArr2[indexPath.row]objectForKey:@"saleprice"];
//                detailWeb.chengbenStr = [dataArr2[indexPath.row]objectForKey:@"costprice"];
//                detailWeb.kucunStr = [dataArr2[indexPath.row]objectForKey:@"numtotalstock"];
//                detailWeb.pid = [dataArr2[indexPath.row]objectForKey:@"userproductid"];
//                [self.navigationController pushViewController:detailWeb animated:YES];
//                
//            }
//
//        }
    }
}
//#pragma mark - 获取二级分类数据数据
//-(void)getChildData11 {
//    [self addAllBtn];
//    //    [dataArr2 removeAllObjects];
//    //    [rightTabV reloadData];
//    if (kNetworkNotReachability) { // 没有网
//        noproV.hidden = NO;
//        [dataArr2 removeAllObjects];
//        [rightTabV reloadData];
//        [[MessShowView share] showWithMess:@"当前网络不给力"];
//        return;
//    }
//    NSString *parentId = @"";
//    if (dataArr1.count > 0) {
//        parentId = [dataArr1[tableMSele] objectForKey:@"categoryid"];
//    }
//    NSString *pddd;
//    if (loadArr.count > 0) {
//        pddd = loadArr[tableMSele];
//    }
//    NSLog(@"%@",pddd);
//    
//    if ([pddd isEqualToString:@"0"]) {
//        [loadArr replaceObjectAtIndex:tableMSele withObject:@"1"];
//        _loadingAniView = [[FeWifiManHub alloc]initWithView:self.view withMode:FeWifiManHubModeOnlyLoader];
//        _loadingAniView.frame = CGRectMake((3*ScreenWidth/4-80)/2, (rightTabV.frame.size.height-30)/2, 80, 80);
//        [rightTabV addSubview:_loadingAniView];
//        [_loadingAniView show];
//    }else {
//        
//    }
////    NSString *url = [NSString stringWithFormat:superMarUrl2,[ShopCommonModel getShopMess].shopuser,parentId];
////    [AFNetworkHandler GETWithAFNByURL:url completion:^(id result) {
////        NSDictionary *childDic = result;
////        if ([childDic[@"r"] isEqualToString:@"T"]) {
////            [_loadingAniView dismiss];
////            dataArr2 = [NSMutableArray array];
////            bigArr = [NSMutableArray array];
////            NSString *pareID = @"";
////            bigArr = [childDic objectForKey:@"childrenoffirstcategory"];
////            NSLog(@"超市界面查询数组bigArr%@",bigArr);
////            for (int i = 0; i < bigArr.count; i++) {
////                [dataArr2 addObjectsFromArray:[bigArr[i] objectForKey:@"products"]];
////                pareID = [bigArr[i] objectForKey:@"parentcategoryid"];
////            }
////            if (dataArr2.count == 0) {
////                noproV.hidden = NO;
////            }else {
////                noproV.hidden = YES;
////            }
////            [rightTabV reloadData];
////        }else {
////            [_loadingAniView dismiss];
////            noproV.hidden = NO;
////            [rightTabV reloadData];
////            [[MessShowView share] showWithMess:childDic[@"m"]];
////        }
////    }];
//}
//- (void)getMessFromLoca {//获取一级分类缓存
//    NSArray *arr = [NSUserDefualManager getValueByKey:@"LC_StorageMarketMess"];
//    if (arr) {
//        for (int i = 0 ; i < arr.count; i ++) {
//            if (i == 0) {
//                dataArr1 = [NSMutableArray array];
//                [dataArr1 addObjectsFromArray:arr[0]];
//            }
//        }
//        [leftTabV reloadData];
//    }
//}
-(void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    if (dataArr1.count > 0) {
        [loadArr removeAllObjects];
        for (int i = 0; i < dataArr1.count; i++) {
            [loadArr insertObject:@"0" atIndex:i];
        }
    }
}

@end
